/**
 * 虚拟旅行数据模型
 */
import { sendNotification } from '@/api/notification';
import { addUserXP, addCurrency } from '@/api/user';
import { getCurrentUserInfo } from '@/utils/auth';

export const TravelModel = {
  async sendAvatarToFriend(userId, friendId, location) {
    const userInfo = getCurrentUserInfo();
    
    // 创建旅行记录
    const travelId = await this.createTravelRecord({
      userId,
      friendId,
      location,
      startTime: new Date(),
      status: 'traveling'
    });
    
    // 通知好友
    await sendNotification(friendId, {
      type: 'avatar_visit',
      message: `${userInfo.userName}的分身来你的${this.getLocationName(location)}参观了!`,
      travelId
    });
    
    return travelId;
  },
  
  async careForVisitor(friendId, travelId) {
    // 获取旅行记录
    const travel = await this.getTravelRecord(travelId);
    
    if (travel.status !== 'traveling') return;
    
    // 增加照顾次数
    await this.updateTravelRecord(travelId, {
      careCount: (travel.careCount || 0) + 1
    });
    
    // 检查是否达到奖励条件
    if (travel.careCount >= 3) {
      await this.giveRewards(travel);
    }
  },
  
  async giveRewards(travel) {
    // 给双方发放奖励
    await Promise.all([
      addUserXP(travel.userId, 50),
      addUserXP(travel.friendId, 50),
      addCurrency(travel.userId, 'coins', 20),
      addCurrency(travel.friendId, 'coins', 20)
    ]);
    
    // 更新旅行状态
    await this.updateTravelRecord(travel.id, {
      status: 'completed',
      endTime: new Date()
    });
  },
  
  // 私有方法
  async createTravelRecord(data) {
    // 实际项目中这里会调用API
    return 'travel_' + Math.random().toString(36).substr(2, 9);
  },
  
  async getTravelRecord(travelId) {
    // 实际项目中这里会调用API
    return {
      id: travelId,
      userId: 'user123',
      friendId: 'user456',
      location: 'city',
      startTime: new Date(),
      status: 'traveling',
      careCount: 0
    };
  },
  
  async updateTravelRecord(travelId, updates) {
    // 实际项目中这里会调用API
    return true;
  },
  
  getLocationName(location) {
    const locations = {
      city: '主城',
      forest: '魔法森林',
      desert: '黄金沙漠'
    };
    return locations[location] || location;
  }
}; 